<template>
  <div class="paper-card" @click="handleClick">
    <div class="paper-icon">
      <el-icon><Document /></el-icon>
    </div>
    <div class="paper-content">
      <el-tooltip 
        :content="paper.name" 
        placement="top" 
        :show-after="500"
        :enterable="false"
        effect="dark"
      >
        <h3 class="paper-title">{{ paper.name }}</h3>
      </el-tooltip>
      <div class="paper-meta" v-if="paper.date">
        <el-icon><Calendar /></el-icon>
        <span>{{ paper.date }}</span>
      </div>
    </div>
  </div>
  
  <!-- 使用Element Plus的Dialog来显示iframe -->
  <el-dialog
    v-model="dialogVisible"
    :title="paper.name"
    width="95%"
    destroy-on-close
    append-to-body
    fullscreen
    class="paper-dialog"
  >
    <div class="iframe-container">
      <iframe :src="paper.src" frameborder="0" @load="handleIframeLoad"></iframe>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue';
import { Document, Calendar } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';

const props = defineProps({
  paper: {
    type: Object,
    required: true
  },
  isAuthorized: {
    type: Boolean,
    default: false
  }
});

const emit = defineEmits(['unauthorized']);

const dialogVisible = ref(false);

const handleClick = () => {
  if (props.isAuthorized) {
    dialogVisible.value = true;
  } else {
    emit('unauthorized');
    ElMessage({
      type: 'warning',
      message: '请先验证管理员密码',
      duration: 2000
    });
  }
};

// 处理iframe加载完成事件
const handleIframeLoad = (event) => {
  try {
    const iframe = event.target;
    const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
    
    // 禁止iframe内容选择和复制
    iframeDoc.addEventListener('mousedown', e => e.preventDefault());
    iframeDoc.addEventListener('selectstart', e => e.preventDefault());
    iframeDoc.addEventListener('contextmenu', e => e.preventDefault());
    iframeDoc.addEventListener('keydown', e => {
      if ((e.ctrlKey || e.metaKey) && ['c','x','v'].includes(e.key)) e.preventDefault();
    });
    
    // 添加CSS禁止选择
    const style = iframeDoc.createElement('style');
    style.textContent = `
      * {
        -webkit-user-select: none !important;
        -moz-user-select: none !important;
        -ms-user-select: none !important;
        user-select: none !important;
      }
    `;
    iframeDoc.head.appendChild(style);
    
    // 禁止iframe内图片拖拽
    iframeDoc.querySelectorAll('img').forEach(img => {
      img.addEventListener('dragstart', e => e.preventDefault());
    });
  } catch (e) {
    console.log('无法访问iframe内容，可能是跨域限制');
  }
};
</script>

<style>


.paper-dialog .el-dialog__body {
  height: calc(100% - 54px) !important;
}
</style>

<style>
.paper-card {
  position: relative;
  margin: 0.5rem;
  padding: 1.5rem;
  border-radius: 0.75rem;
  background-color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  transition: all 0.3s ease;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  border-bottom: 4px solid transparent;
  background-image: linear-gradient(to bottom, white, white), 
                  linear-gradient(to right, #3b82f6, #f97316);
  background-origin: border-box;
  background-clip: padding-box, border-box;
  height: 180px;
  width: 100%;
}

.paper-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.paper-icon {
  flex-shrink: 0;
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  background: linear-gradient(135deg, #3b82f6 0%, #f97316 100%);
}

.paper-icon .el-icon {
  font-size: 1.5rem;
}

.paper-content {
  flex-grow: 1;
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.paper-title {
  font-size: 1rem;
  font-weight: 500;
  margin: 0 0 0.5rem 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.paper-meta {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.875rem;
  color: #6b7280;
}

.iframe-container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  flex: 1;
  left: 0;
  right: 0;
  bottom: 0;
}

.iframe-container iframe {
  width: 100%;
  height: 100%;
  border: 0;
  flex: 1;
}

@media (max-width: 768px) {
  .paper-card {
    height: 160px;
  }
}
</style> 